﻿Namespace EIBData.EIS
    Public NotInheritable Class EIBDataEIS_2_Control
        Inherits EIBDataEIS_2_Base

        Public Enum EIBDataEIS_2_Control_Directions As Byte
            Decrease_Up = 0
            Increase_Down = 1
        End Enum

        Protected _direction As EIBDataEIS_2_Control_Directions = Nothing
        Protected _stepCode As Byte = Nothing


        Sub New(data As Byte())
            If Not CanEncode(data) Then Throw New EIB.Exceptions.EIBDataDecodeException("")

            MyBase._data = data
            _direction = (data(0) And &H8) >> 3
            _stepCode = data(0) And &H7
        End Sub

        Sub New(direction As EIBDataEIS_2_Control_Directions, stepCode As Byte)
            If Not [Enum].IsDefined(GetType(EIBDataEIS_2_Control_Directions), direction) Then Throw New EIB.Exceptions.EIBDataEncodeException("")
            If stepCode > &H7 Then Throw New EIB.Exceptions.EIBDataEncodeException("")

            _direction = direction
            _stepCode = stepCode
            MyBase._data = New Byte() {(If(_direction = EIBDataEIS_2_Control_Directions.Increase_Down, 1, 0) << 3) + (_stepCode And &H7)}
        End Sub


        Public Overrides Function CanEncode(data() As Byte) As Boolean
            Return data IsNot Nothing AndAlso data.Length = EIS_OctetLength
        End Function

        Public Overrides Function ToString() As String
            Return String.Format("Direction: {0}, StepCode: {1}", _direction, _stepCode)
        End Function

        Public Overrides Function ToObject() As Object()
            Return New Object() {_direction, _stepCode}
        End Function

        Public Overrides ReadOnly Property ObjectType As System.Type()
            Get
                Return New Type() {GetType(EIBDataEIS_2_Control_Directions), GetType(Byte)}
            End Get
        End Property

        Public Overrides Function ToInfoList() As List(Of EIB.InfoBase)
            Dim list As New List(Of EIB.InfoBase)

            list.Add(New EIB.Info("Type", EIS_Name))
            list.Add(New EIB.Info("Direction", String.Format("{0} (0x{0:X})", _direction)))
            list.Add(New EIB.Info("StepCode", _stepCode))

            Return list
        End Function

        Public Shared Shadows ReadOnly Property EIS_OctetLength As Byte
            Get
                Return 1
            End Get
        End Property

        Public Shared Shadows ReadOnly Property EIS_Name As String
            Get
                Return "EIS 2 (Control)"
            End Get
        End Property

        Public ReadOnly Property Direction As EIBDataEIS_2_Control_Directions
            Get
                Return _direction
            End Get
        End Property

        Public ReadOnly Property StepCode As Byte
            Get
                Return _stepCode
            End Get
        End Property
    End Class
End Namespace